package com.chengjue.transaction.service.impl;

import com.chengjue.transaction.service.EmailService;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 * 邮件发送
 *
 * @author Kipeng Huang
 * @create 2018-03-02 下午4:11
 */
@Service
public class EmailServiceImpl implements EmailService {
	@Autowired
	private JavaMailSender mailSender;
	@Value("${spring.mail.username}")
	private String userName;

	@Override
	public void sendSimpleMail(String sendTo, String title, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(userName);
		message.setTo(sendTo);
		message.setSubject(title);
		message.setText(content);
		mailSender.send(message);
	}

	@Override
	public void sendAttachmentsMail(String sendTo, String title, String content, List<Pair<String, File>> attachments) {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		try {
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
			mimeMessageHelper.setFrom(userName);
			mimeMessageHelper.setTo(sendTo);
			mimeMessageHelper.setSubject(title);
			mimeMessageHelper.setText(content);

			for (Pair<String, File> pair : attachments) {
				mimeMessageHelper.addAttachment(pair.getKey(),pair.getValue());
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		mailSender.send(mimeMessage);


	}

	@Override
	public void sendTemplateMail(String sendTo, String title, Map<String, Object> content, List<Pair<String, File>> attachments) {

	}
}
